<?php
/**
 * Created by PhpStorm.
 * User: shutao
 * Date: 2018/10/9
 * Time: 10:12
 */

namespace wechat\models;

use tt\common\components\TtActiveRecord;
use ttcommon\base\Arrays;
use yii\db\Migration;

/**
 * 接收普通消息
 * @property integer $id
 * @property integer $msgId
 * @property string $toUserName
 * @property string $fromUserName
 * @property integer $msgType
 * @property string $content
 */
class WeMessage extends TtActiveRecord
{
    public static function tableName()
    {
        return 'we_message';
    }

    public function createTableWithName(Migration $migration, $tableName)
    {
        $migration->createTable($tableName, [
            'id' => $migration->primaryKey()->unsigned(),
            'msgId' => $migration->decimal(64, 0)->unsigned()->comment($this->getAttributeLabel('msgId')),
            'toUserName' => $migration->string(20)->notNull()->comment($this->getAttributeLabel('toUserName')),
            'fromUserName' => $migration->string(30)->notNull()->comment($this->getAttributeLabel('fromUserName')),
            'crateTime' => $migration->integer()->unsigned()->notNull()->defaultValue(0)->comment($this->getAttributeLabel('createTime')),
            'msgType' => $migration->tinyInteger(1)->unsigned()->notNull()->defaultValue(0)->comment($this->getAttributeLabel('msgType')),
            'content' => $migration->text()->notNull()->defaultValue('')->comment($this->getAttributeLabel('content'))
        ]);
    }

    public function attributeLabels()
    {
        return [
            'msgId' => '消息id', // 64位整型
            'toUserName' => '开发者微信号',
            'fromUserName' => '发送方账号', // 一个 OpenID
            'createTime' => '消息创建时间', // 整型
            'msgType' => '类型',
            'content' => '内容'
        ];
    }

    const MSG_TYPE_TEXT = 1;
    const MSG_TYPE_IMAGE = 2;
    const MSG_TYPE_VOICE = 3;
    const MSG_TYPE_VIDEO = 4;
    const MSG_TYPE_SHORT_VIDEO = 5;
    const MSG_TYPE_LOCATION = 6;
    const MSG_TYPE_LINK = 7;

    public static function msgTypeText($msgType = null)
    {
        static $text = [
            self::MSG_TYPE_TEXT => '文本消息',
            self::MSG_TYPE_IMAGE => '图片消息',
            self::MSG_TYPE_VOICE => '语音消息',
            self::MSG_TYPE_VIDEO => '视频消息',
            self::MSG_TYPE_SHORT_VIDEO => '小视频消息',
            self::MSG_TYPE_LOCATION => '地理位置消息',
            self::MSG_TYPE_LINK => '链接消息'
        ];

        return Arrays::constText($text, $msgType);
    }

    public function saveContent($data)
    {

    }
}